package model;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.joda.time.Days;
import org.joda.time.Instant;
import org.joda.time.Interval;
import org.joda.time.LocalDate;

public class Participant implements Serializable {
	private String imei, name, age, job, gender;
	private Date begin, end;

	public Date getBegin() {
		return begin;
	}

	public void setBegin(Date begin) {
		this.begin = begin;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	private ArrayList<User> arraypUser = new ArrayList<>();
	private int numDayLogs;;

	public Participant() {

	}

	public void addUser(User user) {
		this.arraypUser.add(user);
	}

	public ArrayList<User> getArraypUser() {
		return arraypUser;
	}

	public void setArraypUser(ArrayList<User> arraypUser) {
		this.arraypUser = arraypUser;
	}

	public void sortDescendingUserBySms() {
		Collections.sort(this.arraypUser, new Comparator<User>() {
			@Override
			public int compare(User u1, User u2) {
				int num1 = u1.getArrSmsLogs().size();
				int num2 = u2.getArrSmsLogs().size();
				if (num1 == num2) {
					return 0;
				} else if (num1 < num2) {
					return 1;
				} else {
					return -1;
				}
			}

		});
		// System.out.println("SMS");
		// for (int i = 0; i < arraypUser.size(); i++) {
		// System.out.println(arraypUser.get(i).getArrSmsLogs().size());
		// }
		// System.out.println("--------------------------------------");
	}

	public void sortDescendingUserByCall() {
		Collections.sort(this.arraypUser, new Comparator<User>() {
			@Override
			public int compare(User u1, User u2) {
				int num1 = u1.getArrCallLogs().size();
				
				int num2 = u2.getArrCallLogs().size();
				if (num1 == num2) {
					return 0;
				} else if (num1 < num2) {
					return 1;
				} else {
					return -1;
				}
			}

		});
		// System.out.println("CALL");
		// for (int i = 0; i < arraypUser.size(); i++) {
		// System.out.println(arraypUser.get(i).getArrCallLogs().size());
		// }
		// System.out.println("--------------------------------------");
	}

	public void calcBeginAndEndDateCommunicationLog() {

		List<Date> dates = new ArrayList<Date>();
		for (int t = 0; t < this.arraypUser.size(); t++) {
			if (this.arraypUser.get(t).getArrCallLogs() != null
					&& this.arraypUser.get(t).getArrCallLogs().size() > 0) {
				for (int r = 0; r < this.arraypUser.get(t).getArrCallLogs()
						.size(); r++) {
					if (this.arraypUser.get(t).getArrCallLogs().get(r)
							.getDate() != null)
						dates.add(this.arraypUser.get(t).getArrCallLogs()
								.get(r).getDate());
				}
			}
			if (this.arraypUser.get(t).getArrSmsLogs() != null
					&& this.arraypUser.get(t).getArrSmsLogs().size() > 0) {
				for (int r = 0; r < this.arraypUser.get(t).getArrSmsLogs()
						.size(); r++) {
					if (this.arraypUser.get(t).getArrSmsLogs().get(r).getDate() != null)
						dates.add(this.arraypUser.get(t).getArrSmsLogs().get(r)
								.getDate());
				}
			}
		}

		Collections.sort(dates);
		Date m_begin = null;
		Date m_end = null;
		try {
			m_begin = dates.get(0);
			m_end = dates.get(dates.size() - 1);
		} catch (IndexOutOfBoundsException e) {
		}
		this.begin = m_begin;
		this.end = m_end;
//		System.out.println(this.name+" --- "+m_begin + "-" + m_end);
		LocalDate startDate = new LocalDate(m_begin);
		LocalDate endDate = new LocalDate(m_end);
		Days days = Days.daysBetween(startDate, endDate);
		this.numDayLogs = days.getDays();
		// System.out.println(this.numDayLogs);
	}

	public int getNumDayLogs() {
		return numDayLogs;
	}

	public void setNumDayLogs(int numDayLogs) {
		this.numDayLogs = numDayLogs;
	}

	public String getImei() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Participant(String imei, String name, String age, String job,
			String gender) {
		super();
		this.imei = imei;
		this.name = name;
		this.age = age;
		this.job = job;
		this.gender = gender;
	}

}
